﻿using DLL.NET.DB.MS.Drugs.CRUD.Service;

using DLL.NET50.DB.MS.Entity;
using DLL.NET50.DB.PIVAS.Entitys;
using DLL.NET50.DB.PIVAS.Entitys.DTO;
using DLL.NET50.DB.PIVAS.ORM.Service;
using DLL.NET60.DB.MS.Entity.DTO;
using DLL.Standard.Infrastructure.Log;
using System;
using System.Collections.Generic;

namespace DLL.NET50.DB.MS.PIVASAndDicDrug.ORM
{
    public interface IPIVASAndDicDrug
    {
        public DTO_adi_dic_drug GetNewDrugByCode(string drugCode);
    }

    public class CPIVASAndDicDrug : IPIVASAndDicDrug
    {
        private IMSDrugsService _msDrugsService;
        private IPivasDrugsService _pivasDrugsService;
        private ILocalDrugsService _localDrugService;

        public CPIVASAndDicDrug(IMSDrugsService msDrugsService,
            IPivasDrugsService pivasDrugsService,
            ILocalDrugsService localDrugService)
        {
            _msDrugsService = msDrugsService;
            _pivasDrugsService = pivasDrugsService;
            _localDrugService = localDrugService;
        }

        public DTO_adi_dic_drug GetNewDrugByCode(string drugCode)
        {
            try
            {
                DTO_adi_dic_drug drug = new DTO_adi_dic_drug();
                drug.InitializeFlushParam();
                List<DTO_pivas_read_drugs> PivasDrugs = _pivasDrugsService.SearchDrugsByDrugCode(drugCode);
                if (PivasDrugs.Count == 1)
                {
                    drug = PivasLoadToDrug(PivasDrugs[0], drug, drugCode);
                }
                return drug;
            }
            catch (Exception ex)
            {
                LogHelper.Warn("GetNewDrugByCode 函数出现异常：" + ex.Message);
            }

            //看下保存的里面有没有
            return new DTO_adi_dic_drug();
        }

        private DTO_adi_dic_drug PivasLoadToDrug(DTO_pivas_read_drugs PivasDrug, DTO_adi_dic_drug msDrug, string drugcode)
        {
            try
            {
                if (PivasDrug != null)
                {
                    List<DTO_adi_dic_drug> ld = _msDrugsService.SearchDrugsByDrugCode(drugcode);
                    if (0 == ld.Count)
                    {
                        //本地不存在相同编码的药
                        string uniqueCode = PivasDrug.GetUniqueCode();
                        List<DTO_adi_dic_drug> mld = _msDrugsService.SearchByUniqueCode(uniqueCode);
                        if (mld.Count == 1)
                        {
                            //本地存在，相同的药，但是药品编码不同相同
                            msDrug = mld[0];
                            //将新的PIVAS药品信息，复制给已经存在药
                            msDrug = PivasCopyToDicDrug(PivasDrug, msDrug);
                        }
                        else
                        {
                            //本地不存在
                            List<DTO_adi_dic_drug> sld = _localDrugService.SearchByUniqueCode(uniqueCode);
                            if (sld.Count == 1)
                            {
                                //读取savedDrug里面存在药，直接加载过来
                                msDrug = (DTO_adi_dic_drug)sld[0].Clone();
                                msDrug = PivasCopyToDicDrug(PivasDrug, msDrug);
                            }
                            else
                            {
                                msDrug = PivasCopyToDicDrug(PivasDrug, msDrug);
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                LogHelper.Warn("PivasLoadToDrug 函数出现异常：" + ex.Message);
            }

            return msDrug;
        }

        public DTO_pivas_read_drugs DicDrugToPivasDrug(DTO_adi_dic_drug dicDrug)
        {
            DTO_pivas_read_drugs dstDrug = new DTO_pivas_read_drugs();
            dstDrug.DRUG_CODE = dicDrug.DRUG_CODE;
            dstDrug.DRUG_NAME = dicDrug.DRUG_NAME;
            dstDrug.DOSE = Convert.ToDecimal(dicDrug.DOSE);
            dstDrug.SPECIFICATION = dicDrug.SPECIFICATION;
            dstDrug.DOSE_UNIT = dicDrug.DOSE_UNIT;
            dstDrug.MAFC_NAME = dicDrug.MAFC_NAME;
            dstDrug.PY_CODE = dicDrug.PY_CODE;
            return dstDrug;
        }

        public DTO_adi_dic_drug PivasCopyToDicDrug(DTO_pivas_read_drugs newPivasDrug, DTO_adi_dic_drug oldDicDrug)
        {
            DTO_adi_dic_drug dstDrug = (DTO_adi_dic_drug)oldDicDrug.Clone();
            dstDrug.DRUG_CODE = newPivasDrug.DRUG_CODE;
            dstDrug.DRUG_NAME = newPivasDrug.DRUG_NAME;
            dstDrug.DOSE = (float)newPivasDrug.DOSE;
            dstDrug.SPECIFICATION = newPivasDrug.SPECIFICATION;
            dstDrug.DOSE_UNIT = newPivasDrug.DOSE_UNIT;
            dstDrug.MAFC_NAME = newPivasDrug.MAFC_NAME;
            dstDrug.PY_CODE = newPivasDrug.PY_CODE;
            dstDrug.Drug_UniqueCode = dstDrug.GetUniqueCode();
            dstDrug.SetFlushParamUniqueCode();
            //dstDrug.ApprovalNumber = newPivasDrug.APPROVAL_NUMBER;
            return dstDrug;
        }
    }
}